package com.lib.online.model;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.persistence.*;
import javax.xml.bind.annotation.*;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

import org.codehaus.jackson.annotate.JsonIgnore;
import org.joda.time.DateTime;

/**
 * @author ievgen_s
 */

@XmlRootElement
@XmlAccessorType(XmlAccessType.PROPERTY)
@XmlType(propOrder = { "author_id",
	"fName",
	"lName",
	"birth", 
	"death"
	})
@Entity
@Table(name="authors")
@Cacheable
public class Author implements Serializable, Comparable<Author> {

	
    private static final long serialVersionUID = -5702895146786790825L;
    private Integer author_id;
    private String fName;
    private String lName;
   // @XmlElement(name = "birth", type = DateTime.class, required=true)
    private DateTime birth;
   // @XmlElement(name = "death", type = DateTime.class)
    private DateTime death;
    @XmlTransient
    @JsonIgnore
    private List<Reading> books;

    public Author() {
    }

   
    public Author(Integer author_id, 
	    String fName, 
	    String lName,
	    Date birth,
	    Date death) {
    	
    	this.author_id = author_id;
    	this.fName = fName;
    	this.lName = lName;
    	this.birth = new DateTime(birth);
	this.death = new DateTime(death);
    }

  
    @Id 
	@Column(name="author_id")
    @GeneratedValue  
    public Integer getAuthor_id() {
		return author_id;
	}

	public void setAuthor_id(Integer author_id) {
		this.author_id = author_id;
	}

	@Column(name="first_name")
	public String getfName() {
	return fName;
    }

    public void setfName(String fName) {
	this.fName = fName;
    }

    @Column(name="last_name")
    public String getlName() {
	return lName;
    }

    public void setlName(String lName) {
	this.lName = lName;
    }

    @Temporal(TemporalType.DATE)
    @Column(name="birth")
    public Date getBirth() {
	return birth.toDate();
    }

    public void setBirth(Date birth) {
	this.birth = new DateTime(birth);
    }

    @Temporal(TemporalType.DATE)
    @Column(name="death")
    public Date getDeath() {
	return death.toDate();
    }

    public void setDeath(Date death) {
	this.death = new DateTime(death);
    }

    @OneToMany(mappedBy="author", fetch=FetchType.EAGER)
    @XmlTransient
    @JsonIgnore    
    public List<Reading> getBooks() {
		return books;
	}


	public void setBooks(List<Reading> books) {
		this.books = books;
	}


	@Override
    public String toString() {
	return fName + "  " + lName;
    }

    @Override
    public int compareTo(Author o) {
	return this.fName.compareTo(o.fName);
    }

}